package org.flint92.springboot.mybatis.handler;

import com.alibaba.fastjson.JSON;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.flint92.springboot.mybatis.domain.Address;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author flint92
 */
public class AddressTypeHandler implements TypeHandler<Address> {

    @Override
    public void setParameter(PreparedStatement ps, int i, Address parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, JSON.toJSONString(parameter));
    }

    @Override
    public Address getResult(ResultSet rs, String columnName) throws SQLException {
        String address = rs.getString(columnName);
        return JSON.toJavaObject(JSON.parseObject(address), Address.class);
    }

    @Override
    public Address getResult(ResultSet rs, int columnIndex) throws SQLException {
        String address = rs.getString(columnIndex);
        return JSON.toJavaObject(JSON.parseObject(address), Address.class);
    }

    @Override
    public Address getResult(CallableStatement cs, int columnIndex) throws SQLException {
        return null;
    }
}
